package com.air.ota.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.common.exception.OutofPageBoundException;
import com.air.common.util.QueryCondition;
import com.air.common.util.QueryCondition4Update;
import com.air.common.vo.PageResultListVO;
import com.air.ota.mo.DepositTrans;
import com.air.ota.mo.InboundMsg;
import com.air.ota.mo.OutboundMsg;
import com.air.ota.service.IDepositSyncHandlerService;
import com.air.ota.service.IDepositTransService;
import com.air.ota.service.ILastDepositTransHandlerService;
import com.air.ota.service.IOutboundMsgService;
import com.air.ota.service.IPkgSequenceService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.vo.UTKDepositSyncResultDownlinkVO;
import com.air.ota.vo.UTKDepositSyncUplinkVO;

@Service("depositSyncHandlerService")
public class DepositSyncHandlerServiceImp implements IDepositSyncHandlerService {
	
	@Autowired
	ILastDepositTransHandlerService lastDepositTransHandlerService;
	@Autowired
	IPkgSequenceService pkgSequenceService;
	@Autowired
	IOutboundMsgService outboundMsgService;
	
	
	@Override
	public void process(UTKDepositSyncUplinkVO pkgVO, InboundMsg inboundMsg)
			throws Exception {
		System.out.println("------------Ready to Process DepositSync Request");
		boolean processResult = lastDepositTransHandlerService.process(pkgVO.getLastTransData(), inboundMsg.getPhoneNumber());
		
		byte result= 0x00;
		if (!processResult){
			result = 0x01;
		}
		
		
		UTKDepositSyncResultDownlinkVO downlinkVO = new UTKDepositSyncResultDownlinkVO(
				pkgVO.getCardSequence(),pkgSequenceService.getNextSequence( inboundMsg.getPhoneNumber()),
				pkgVO.getCardAppSequence(),pkgVO.getSyncSequence(),result
			); 
		
		byte[] responsePkg=downlinkVO.buildPkg();
		
		
		OutboundMsg outboundMsg = new OutboundMsg();
		outboundMsg.setContent(HexStringUtil.bytesToHexString(responsePkg));
		outboundMsg.setDest(0);
		outboundMsg.setPhoneNumber(inboundMsg.getPhoneNumber());
		outboundMsg.setSendDate(System.currentTimeMillis());
		outboundMsg.setTransId(-1);
		outboundMsg.setNeedRetry(0);
		outboundMsg.setMsgType(AppConstant._outbound_msg_type_depositSyncResponse);
		outboundMsgService.insert(outboundMsg);
		
	}

	
}
